package com.useeinfo.oa.modules.maintaining.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.enums.AuditTypeEnum;
import com.useeinfo.oa.modules.maintaining.entity.ApplyOrder;
import com.useeinfo.oa.modules.maintaining.enums.ApplyTypeEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("applyOrderDao")
public class ApplyOrderDao implements CrudDao<ApplyOrder> {
    private final static Logger logger = LoggerFactory.getLogger(ApplyOrderDao.class);
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        conditionHash.put("s.maintainOrder is not null and 1= ?{paramIndex} ", 1);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String maintainOrderId = queryHash.get("maintainOrderId");
        if (!StringUtils.isEmpty(maintainOrderId)) {
            conditionHash.put("(s.maintainOrder.id = ?{paramIndex}) ", StringConverters.null2Long(maintainOrderId));
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(s.enterprise.id = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        String name = queryHash.get("name");
        if (!StringUtils.isEmpty(name)) {
            conditionHash.put("(s.maintainOrder.orderName like ?{paramIndex}) ", "%"+name+"%");
        }
        String startTime = queryHash.get("startTime");
        if (!StringUtils.isEmpty(startTime)) {
            conditionHash.put("(s.createDate >= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",startTime));
        }
        String endTime = queryHash.get("endTime");
        if (!StringUtils.isEmpty(endTime)) {
            conditionHash.put("(s.createDate <= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",endTime));
        }
        String applyType = queryHash.get("applyType");
        if (StringUtils.isNotBlank(applyType)) {
            String[] statusArray = applyType.split(",");
            for (int i = 0; i < statusArray.length; i++) {
                if (i == 0) {
                    String k = statusArray.length == 1 ? "applyTypeEnum = ?{paramIndex} " : "(applyTypeEnum = ?{paramIndex} ";
                    conditionHash.put(k, ApplyTypeEnum.valueOf(statusArray[i].trim()));
                } else if (i == statusArray.length - 1) {
                    conditionHash.put("or applyTypeEnum = ?{paramIndex}) ", ApplyTypeEnum.valueOf(statusArray[i].trim()));
                } else {
                    String k = String.format("or applyTypeEnum = ?{paramIndex} %" + i + "s", "");
                    conditionHash.put(k, ApplyTypeEnum.valueOf(statusArray[i].trim()));
                }
            }
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from ApplyOrder s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<ApplyOrder> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from ApplyOrder s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<ApplyOrder> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, ApplyOrder.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public ApplyOrder findModel(Long id) {
        return entityManager.find(ApplyOrder.class, id);
    }

    @Override
    public Integer add(ApplyOrder model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("ApplyOrderDaoImpl添加applyOrder成功！");
        return 1;
    }

    @Override
    public Integer update(ApplyOrder model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        ApplyOrder applyRecord = entityManager.find(ApplyOrder.class, id);
        entityManager.remove(applyRecord);
        return 1;
    }

    @Override
    public Integer deleteLogic(ApplyOrder model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
